package com.dibang.app.mvp.util;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dibang.app.R;
import com.gyf.immersionbar.ImmersionBar;

import static android.widget.LinearLayout.VERTICAL;

/**
 * Created by wxl on 2019/7/1.
 */

public class ViewUtil {


    public static View generateFragmentView(@NonNull Activity act, @LayoutRes int layoutId) {
        LinearLayout container = generateContainsLayout(act);
        View contentLayout = LayoutInflater.from(act).inflate(layoutId, null, false);
        LinearLayout parentLayout = generateParentLayout(act, contentLayout);
        container.addView(parentLayout);
        return container;
    }



    public static View generateActivityView(@NonNull Activity act, @LayoutRes int layoutId) {
        ImmersionBar immersionBar = ImmersionBar.with(act)
                .transparentBar()
                .statusBarColor(android.R.color.white)
                .autoDarkModeEnable(true,0.2f);
        LinearLayout container = generateContainsLayout(act);
        View contentLayout = LayoutInflater.from(act).inflate(layoutId, null, false);
        LinearLayout parentLayout = generateParentLayout(act, contentLayout);
        container.addView(parentLayout);
        if (ImmersionBar.hasNavigationBar(act) && ImmersionBar.isNavigationAtBottom(act)) {
            ColorDrawable drawable = (ColorDrawable) contentLayout.getBackground();
            View buttonView = generateButtonView(act, drawable.getColor(), immersionBar);
            container.addView(buttonView);
        }
        immersionBar.init();
        return container;
    }


    public static View generateActivityViewContainTopView(@NonNull Activity act, @LayoutRes int layoutId,@ColorRes int statusColor) {
        ImmersionBar immersionBar = ImmersionBar.with(act).transparentBar().statusBarDarkFont(false);
        LinearLayout container = generateContainsLayout(act);
        View topView = generateTopView(act);
        notifyImmersionBarChanged(immersionBar,container,topView,statusColor);
        View contentLayout = LayoutInflater.from(act).inflate(layoutId, null, false);
        LinearLayout parentLayout = generateParentLayout(act, contentLayout);
        container.addView(parentLayout);
        if (ImmersionBar.hasNavigationBar(act) && ImmersionBar.isNavigationAtBottom(act)) {
            ColorDrawable drawable = (ColorDrawable) contentLayout.getBackground();
            View buttonView = generateButtonView(act, drawable.getColor(), immersionBar);
            container.addView(buttonView);
        }
        immersionBar.init();//指定标题栏view.init();
        return container;
    }


    private static void notifyImmersionBarChanged(ImmersionBar immersionBar,LinearLayout container,View topView,@ColorRes int statusColor){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ((Activity)container.getContext()).getWindow().setStatusBarColor(container.getContext().getResources().getColor(android.R.color.white));
            ((Activity)container.getContext()).getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            immersionBar.keyboardEnable(true)
                    .navigationBarWithKitkatEnable(false)
                    .titleBar(container)
                    .keyboardEnable(true)
                    .statusBarDarkFont(true);
        } else {
            notifyStatusBarChanged(immersionBar,container,topView,statusColor);
        }
    }

    private static void notifyStatusBarChanged(ImmersionBar immersionBar,LinearLayout container,View topView,@ColorRes int statusColor) {
        topView.setBackgroundColor(ContextCompat.getColor(container.getContext(),statusColor));
        container.addView(topView);
        immersionBar.keyboardEnable(true)
                    .statusBarDarkFont(true)
                    .statusBarColor(statusColor);
    }


    public static View generateActivityViewContainerTitleView(@NonNull Activity act, @LayoutRes int layoutId,@ColorRes int statusColor) {
        ImmersionBar immersionBar = ImmersionBar.with(act)
                .transparentBar()
                .statusBarColor(android.R.color.white)
                .autoDarkModeEnable(true,0.2f);
        LinearLayout container = generateContainsLayout(act);
        LinearLayout titleBarLayout = generateTitleBarLayout(act,immersionBar,statusColor);
        container.addView(titleBarLayout);
        View contentLayout = LayoutInflater.from(act).inflate(layoutId, null, false);
        LinearLayout parentLayout = generateParentLayout(act, contentLayout);
        container.addView(parentLayout);
        if (ImmersionBar.hasNavigationBar(act) && ImmersionBar.isNavigationAtBottom(act)) {
            if(contentLayout.getBackground() instanceof ColorDrawable) {
                ColorDrawable drawable = (ColorDrawable) contentLayout.getBackground();
                View buttonView = generateButtonView(act, drawable.getColor(), immersionBar);
                container.addView(buttonView);
            } else {
                View buttonView = generateButtonView(act, Color.parseColor("#ffffffff"), immersionBar);
                container.addView(buttonView);
            }
        }
        immersionBar.init();
        return container;
    }



    private static LinearLayout generateTitleBarLayout(@NonNull final Activity act,ImmersionBar immersionBar,@ColorRes int statusColor) {
        View topView = generateTopView(act);
        int height = ImmersionBar.getStatusBarHeight(act);
        LinearLayout titleLayout = new LinearLayout(act);
        titleLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DPUtil.dip2px(act, 51) + height));
        titleLayout.setOrientation(VERTICAL);
        titleLayout.setBackgroundColor(ContextCompat.getColor(act, android.R.color.white));
        //titleLayout.addView(topView);
        notifyImmersionBarChanged(immersionBar,titleLayout,topView,statusColor);
        RelativeLayout layout = new RelativeLayout(act);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DPUtil.dip2px(act, 50));
        layout.setLayoutParams(params);
        layout.setGravity(Gravity.CENTER_VERTICAL);
        RelativeLayout backView = new RelativeLayout(act);
        backView.setGravity(Gravity.CENTER);
        backView.setId(R.id.activity_back_layout_id);
        backView.setLayoutParams(new ViewGroup.LayoutParams(DPUtil.dip2px(act, 40), DPUtil.dip2px(act, 50)));
        View backIcon = new View(act);
        backIcon.setLayoutParams(new ViewGroup.LayoutParams(DPUtil.dip2px(act, 10), DPUtil.dip2px(act, 18)));
        backIcon.setBackgroundResource(R.mipmap.back);
        backView.addView(backIcon);
        layout.addView(backView);
        TextView title = new TextView(act);
        title.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        title.setTextSize(18);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(ContextCompat.getColor(act, android.R.color.black));
        title.setId(R.id.activity_title_id);
        layout.addView(title);
        TextView menu = new TextView(act);
        RelativeLayout.LayoutParams menuParam = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        menuParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        menuParam.rightMargin = DPUtil.dip2px(act, 10);
        menu.setTextSize(14);
        menu.setGravity(Gravity.CENTER);
        menu.setLayoutParams(menuParam);
        menu.setId(R.id.activity_menu_id);
        layout.addView(menu, menuParam);
        backView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (act != null && !act.isFinishing()) {
                    act.finish();
                }
            }
        });
        titleLayout.addView(layout);
        View divider = new View(act);
        divider.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DPUtil.dip2px(act, 1)));
        divider.setBackgroundColor(Color.parseColor("#fafafa"));
        titleLayout.addView(divider);
        return titleLayout;
    }

    private static LinearLayout generateParentLayout(@NonNull Activity act, View contentLayout) {
        LinearLayout container = new LinearLayout(act);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        container.setLayoutParams(params);
        container.setOrientation(VERTICAL);
        params.weight = 1;
        container.addView(contentLayout, params);
        return container;
    }


    private static LinearLayout generateContainsLayout(@NonNull Activity act) {
        LinearLayout container = new LinearLayout(act);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        container.setLayoutParams(params);
        container.setOrientation(VERTICAL);
        return container;
    }


    private static View generateTopView(@NonNull Activity act) {
        View topView = new View(act);
        int height = ImmersionBar.getStatusBarHeight(act);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        topView.setLayoutParams(params);
        topView.setBackgroundColor(ContextCompat.getColor(act, android.R.color.black));
        return topView;
    }


    private static View generateButtonView(@NonNull Activity act, @ColorInt int color, ImmersionBar immersionBar) {
        View buttonView = new View(act);
        int height = ImmersionBar.getNavigationBarHeight(act);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        buttonView.setLayoutParams(params);
        buttonView.setBackgroundColor(color);
        immersionBar.navigationBarColorInt(ContextCompat.getColor(act, android.R.color.white), ContextCompat.getColor(act, android.R.color.white), 1.0f);
        return buttonView;
    }
}
